/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package op;

import core.CreateConta;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Jônatas
 */
public class CreateContaPabxTaritron implements CreateConta {

    public static Pattern patternLigacao = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{6}:"
            + "[0-9]{2}:[0-9]{2}");

    public ContaTelefonica createConta(String[] fullFile) throws Exception {

        Telefone origem = null;

        ContaTelefonica contaTelefonica = new ContaTelefonica();

        contaTelefonica.contaMensal = new ArrayList<EntradaAbstrata>();

        for (String line : fullFile) {

            line = line.trim();

            if (line.isEmpty()) {
                continue;
            }

            if (line.toLowerCase().startsWith("ramal")) {
                StringTokenizer tokenizer = new StringTokenizer(line);
                tokenizer.nextToken();

                String numRamal = tokenizer.nextToken().replace(":", "");

                String nome = "";

                while (tokenizer.hasMoreTokens()) {
                    nome += tokenizer.nextToken() + " ";
                }

                origem = new Telefone(numRamal + " - " + nome.trim(), true);

                continue;
            }

            if (origem == null) {
                continue;
            }

            Matcher matchLigacao = patternLigacao.matcher(line);

            if (!matchLigacao.find()) {
                continue;
            }

            Map<String, Object> parametersMap = new HashMap<String, Object>();
            parametersMap.put("line", line);
            parametersMap.put("origem", origem);

            EntradaAbstrata entrada = new QuebraStringPabxTaritron().quebraString(parametersMap);

            contaTelefonica.contaMensal.add(entrada);

        }



        Collections.sort(contaTelefonica.contaMensal, new ComparatorEntradaAbstrataPorTelefone());

        contaTelefonica.contaResumida();

        return contaTelefonica;

    }
}
